<template>
  <div>
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
      <div style="margin:15px 0;">
        <span style="font-size:35px;font-weight:600;">填写文章</span>
      </div>

      <el-form-item prop="title" label="标题">
        <el-input v-model="dynamicValidateForm.title"></el-input>
      </el-form-item>
      <el-form-item v-for="(domain, index) in dynamicValidateForm.domains" :label="'段落' + (index+1)" :key="domain.key"
        :prop="'domains.' + index + '.value'">
        <el-input :autosize="{ minRows: 2}" type="textarea" v-model="domain.value"></el-input>
        <!-- <el-button @click.prevent="removeDomain(domain)">删除</el-button> -->
      </el-form-item>
      <div style="margin:15px 0;">
        <span style="font-size:35px;font-weight:600;">选择上传配图</span>
      </div>
      <div class="imgItem">
        <div v-for="(item,index) in imgList" :key="index">
          <el-image class="img" fit="cover" :src="item.url" title="点击移除" @click="deleteimg(index)">
          </el-image>
          <el-form-item label="输入图片标题">
            <el-input v-model="item.name" class="ipt">
            </el-input>
          </el-form-item>
        </div>
      </div>
      <el-upload action="#" list-type="picture-card" :show-file-list='false' :auto-upload="false"
        :on-change="changefile">
        <i slot="default" class="el-icon-plus">选择</i>
      </el-upload>
      <div style="margin:15px 0;">
        <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
        <!-- <el-button @click="addDomain">新增段落</el-button> -->
        <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
      </div>

    </el-form>
  </div>
</template>

<script>
export default {
  name: 'News',
  data () {
    return {
      dynamicValidateForm: {
        domains: [{
          value: '这是第一行<span>这是第2行'
        }],
        title: ''
      },
      imgList: []
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var aStr = this.dynamicValidateForm.domains[0].value
          let re = /\n/g
          var str = aStr.replace(re, '<span>')
          alert(str)
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    removeDomain (item) {
      var index = this.dynamicValidateForm.domains.indexOf(item)
      if (index !== -1) {
        this.dynamicValidateForm.domains.splice(index, 1)
      }
    },
    addDomain () {
      this.dynamicValidateForm.domains.push({
        value: '',
        key: Date.now()
      })
    },
    changefile (file, fileList) {
      this.imgList = fileList
      console.log(this.imgList)
    },
    deleteimg (index) {
      this.imgList.splice(index, 1)
      console.log(this.imgList)
    },
    changeText (message) {
      var text = message
      var re = /<span>/g
      var str = text.replace(re, '\n')
      this.dynamicValidateForm.domains[0].value = str
      return str
    }
  },
  mounted () {
    this.changeText('这是第一行<span>这是第2行<span>这是第3行')
  }
}
</script>

<style scoped>
.imgItem {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  overflow: hidden;
}
.img {
  width: 450px;
  height: 300px;
}
.ipt {
  width: 350px;
}
</style>
